package lang4j.parser;

import java.util.Map;
import java.util.HashMap;

/**
 * User: felix
 * Date: 22.11.2005
 * Time: 08:11:57
 */
public class IdentifierUtils {
    public final static Map charactersToIdentifiers;
    static{
        charactersToIdentifiers=new HashMap();
        putChar(',', "COMMA");
        putChar(':',"COLON");
        putChar(';',"SEMI");
        putChar('.',"DOT");
        putChar('-',"MINUS");
        putChar('+',"PLUS");
        putChar('*',"STAR");
        putChar('/',"DIV");
        putChar('(',"LPAREN");
        putChar(')',"RPAREN");
        putChar('[',"LBRACKET");
        putChar(']',"RBRACKET");
        putChar('{',"LCURL");
        putChar('}',"RCURL");
        putChar('#',"HASH");
        putChar('\\',"BACK_SLASH");
        putChar('<',"LT");
        putChar('>',"GT");
        putChar('=',"EQUALS");
        putChar('$',"DOLLAR");
        putChar('!',"EXCLAMATION");
        putChar('^',"CIRCUMFLEX");
        putChar('?',"QUESTION");
        putChar('@',"AT");


    }

    public static String getIdentifier(char c){
        return (String) charactersToIdentifiers.get(new Character(c));
        
    }


    private static void putChar(final char ch, final String ident) {
        charactersToIdentifiers.put(new Character(ch),ident);
    }


    public static String camelToUnderscore(String s){
        if (s.toUpperCase().equals(s)){
            return s;
        }
        String[] words = s.split("(?=[A-Z])");
        StringBuffer buffer=new StringBuffer(s.length()+words.length);
        for (int i = 0; i < words.length; i++) {
            String word = words[i];
            if (word.equals("")) continue;
            buffer.append(word.toUpperCase());
            if ((i<words.length-1) ){
                buffer.append("_");
            }
        }
        return buffer.toString();
    }

    public static void main(String[] args) {
        System.out.println(camelToUnderscore("myTestData"));
    }
}
